<template>
  <div class="strut-div" id="worldMap"></div>
</template>

<script>
  import echarts from 'echarts';
  import '../../../../static/world.js'
  export default {
    data() {
      return {
        charts: null,
        option: null,
      }
    },
    mounted() {
      var geoCoordMap = {
        洪洞: [111.681922,36.259562],
        尼日利亚: [-4.388361, 11.186148],
        美国洛杉矶: [-118.24311, 34.052713],
        香港: [114.195466, 22.282751],
        美国芝加哥: [-87.801833, 41.870975],
        加纳库马西: [-4.62829, 7.72415],
        英国曼彻斯特: [-1.657222, 51.886863],
        德国汉堡: [10.01959, 54.38474],
        哈萨克斯坦阿拉木图: [45.326912, 41.101891],
        俄罗斯伊尔库茨克: [89.116876, 67.757906],
        巴西: [-48.678945, -10.493623],
        埃及达米埃塔: [31.815593, 31.418032],
        西班牙巴塞罗纳: [2.175129, 41.385064],
        柬埔寨金边: [104.88659, 11.545469],
        意大利米兰: [9.189948, 45.46623],
        乌拉圭蒙得维的亚: [-56.162231, -34.901113],
        莫桑比克马普托: [32.608571, -25.893473],
        阿尔及利亚阿尔及尔: [3.054275, 36.753027],
        阿联酋迪拜: [55.269441, 25.204514],
        匈牙利布达佩斯: [17.108519, 48.179162],
        澳大利亚悉尼: [150.993137, -33.675509],
        美国加州: [-121.910642, 41.38028],
        澳大利亚墨尔本: [144.999416, -37.781726],
        墨西哥: [-99.094092, 19.365711],
        加拿大温哥华: [-123.023921, 49.311753]
      };
      var BJData = [
        [{
          name: "尼日利亚",
          value: 9100
        }, {
          name: "洪洞"
        }],
        [{
          name: "美国洛杉矶",
          value: 2370
        }, {
          name: "洪洞"
        }],
        [{
          name: "香港",
          value: 3130
        }, {
          name: "洪洞"
        }],
        [{
          name: "美国芝加哥",
          value: 2350
        }, {
          name: "洪洞"
        }],
        [{
          name: "加纳库马西",
          value: 5120
        }, {
          name: "洪洞"
        }],
        [{
          name: "英国曼彻斯特",
          value: 3110
        }, {
          name: "洪洞"
        }],
        [{
          name: "德国汉堡",
          value: 6280
        }, {
          name: "洪洞"
        }],
        [{
          name: "哈萨克斯坦阿拉木图",
          value: 7255
        }, {
          name: "洪洞"
        }],
        [{
          name: "俄罗斯伊尔库茨克",
          value: 8125
        }, {
          name: "洪洞"
        }],
        [{
          name: "巴西",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "埃及达米埃塔",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "西班牙巴塞罗纳",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "柬埔寨金边",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "意大利米兰",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "乌拉圭蒙得维的亚",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "莫桑比克马普托",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "阿尔及利亚阿尔及尔",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "阿联酋迪拜",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "匈牙利布达佩斯",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "澳大利亚悉尼",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "美国加州",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "澳大利亚墨尔本",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "墨西哥",
          value: 3590
        }, {
          name: "洪洞"
        }],
        [{
          name: "加拿大温哥华",
          value: 3590
        }, {
          name: "洪洞"
        }]
      ];
      var convertData = function(data) {
        var res = [];
        for (var i = 0; i < data.length; i++) {
          var dataItem = data[i];
          var fromCoord = geoCoordMap[dataItem[0].name];
          var toCoord = geoCoordMap[dataItem[1].name];
          if (fromCoord && toCoord) {
            res.push([{
                coord: fromCoord,
                value: dataItem[0].value
              },
              {
                coord: toCoord
              }
            ]);
          }
        }
        return res;
      };

      var series = [];
      [
        ["洪洞", BJData]
      ].forEach(function(item, i) {
        series.push(
          {
            type: "lines",
            zlevel: 2,
            effect: {
              show: true,
              period: 5, //箭头指向速度，值越小速度越快
              trailLength: 0.11, //特效尾迹长度[0,1]值越大，尾迹越长重
              symbol: "arrow", //箭头图标
              symbolSize: 3 //图标大小
            },
            lineStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: '#58B3CC'
                }, {
                  offset: 1,
                  color: '#F58158'
                }], false),
                width: 1,
                opacity: 0.1,
                curveness: 0.1
              }
            },

            data: convertData(item[1]),
            tooltip: {
              show: false
            }
          },

          {
            type: "effectScatter",
            coordinateSystem: "geo",
            zlevel: 2,
            rippleEffect: {
              //涟漪特效
              period: 8, //动画时间，值越小速度越快
              brushType: "fill", //波纹绘制方式 stroke, fill
              scale: 5 //波纹圆环最大限制，值越大波纹越大
            },
            label: {
              normal: {
                show: true,
                position: "right", //显示位置
                offset: [10, 0], //偏移设置
                formatter: "{b}", //圆环显示文字
                color: "#ffffff",
              },
              emphasis: {
                show: true
              }
            },
            symbol: "circle",
            symbolSize: function(val) {
              return val[2] / 800; //圆环大小
            },
            itemStyle: {
              normal: {
                color: '#FFF'
              }
            },
            data: item[1].map(function(dataItem) {
              return {
                name: dataItem[0].name,
                value: geoCoordMap[dataItem[0].name].concat([dataItem[0].value])
              };
            }),
            tooltip: {
              show: false
            }
          },
          //被攻击点
          {
            type: "scatter",
            coordinateSystem: "geo",
            zlevel: 2,
            rippleEffect: {
              period: 4,
              brushType: "stroke",
              scale: 4
            },
            label: {
              normal: {
                show: true,
                position: "right",
                color: "#ffffff",
                formatter: "{b}",
                textStyle: {
                  color: "#ffffff",
                  fontSize: 24
                }
              },
              emphasis: {
                show: true
              }
            },
            symbol: "pin",
            symbolSize: 36,
            itemStyle: {
              normal: {
                color: "#00f0ff"
              }
            },
            data: [{
              name: item[0],
              value: geoCoordMap[item[0]].concat([100])
            }]
          }
        );
      });
      
     this.option = {
        backgroundColor:'', //设置无背景色
        tooltip: {
          trigger: "item",
          // backgroundColor: "#1540a1",
          // borderColor: "#FFFFCC",
          showDelay: 0,
          hideDelay: 0,
          enterable: true,
          transitionDuration: 0,
          extraCssText: "z-index:100",
          formatter: function(params, ticket, callback) {
            //根据业务自己拓展要显示的内容
            var res = "";
            var name = params.name;
            var value = params.value[params.seriesIndex + 1];
            res =
              "<span style='color:#fff;'>" +
              name +
              "</span><br/>数据：" +
              value;
            return res;
          }
        },
        visualMap: {
          //图例值控制
          min: 0,
          max: 10000,
          show: false,
          calculable: true,
          color: ["#FFFC00", "#00dffc"],
          textStyle: {
            color: "#fff"
          },
        },
        geo: {
          map: "world",
          label: {
            emphasis: {
              show: false
            }
          },
          aspectScale:0.9,
          roam: false, //是否允许缩放
          layoutCenter: ["50%", "50%"], //地图位置
          layoutSize: "225%",
          itemStyle: {
            normal: {
              areaColor: 'rgba(1, 108, 143, 1)',
              borderColor: '#8F98A6'
            },
            emphasis: {
              areaColor: '#00f0ff'
            }
          }
        },
        series: series
      };

       this.charts = echarts.init(document.getElementById('worldMap'));
       this.charts.setOption(this.option)
    },
    methods: {
      initWorldMap() {
        this.charts.setOption(this.option)
      }
    },
  }
</script>

<style>
</style>
